package com.xml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.db.ColumnInfoBean;
import com.excel.read.Config;
import com.excel.read.DBTable;
/**
 * 一条数据库操作记录
 * 
 * @author Administrator
 *
 */
public class Record {
	/**
	 * 如果记录的自然主键已经被更新（由原来系统的自然主键更新为本地系统的自然主键）
	 * 则flag设置为true，否则为flase
	 */
	private boolean flag = false;
	
	/**
	 * 表名
	 */
	private String tableName;
	
	public String op;
	
	public Map<String, Object> fieldMap = new HashMap<String, Object>();
	
	public long storeFrom;
	
	public void put(String key, Object value) {
		fieldMap.put(key, value);
	}
	
	public List<ColumnInfoBean> getNoPKColumnInfos() {
		DBTable dbTable = (DBTable) Config.allTable.get(this.tableName);
		List<ColumnInfoBean> list = new ArrayList<ColumnInfoBean>();
		for(String fieldName : getNoPKNameList()) {
			ColumnInfoBean aColumnInfoBean = dbTable.fieldMap.get(fieldName);
			list.add(aColumnInfoBean);
		}		
		return list;
	}
	
	public   List<String> getNoPKNameList() {
		DBTable dbTable = (DBTable) Config.allTable.get(this.tableName);
	    List<String> names = new ArrayList<String>();
	    String pkName = dbTable.getPk().getName();
		for(String fieldName : getFieldNames()) {
			if(pkName.equalsIgnoreCase(fieldName)) {				
			}else {
				names.add(fieldName);
			}
		}
		return names;
	}
	
	
	public   List<String> getPKNameList() {
		DBTable dbTable = (DBTable) Config.allTable.get(this.tableName);
	    List<String> names = new ArrayList<String>();
	    String pkName = dbTable.getPk().getName();
		for(String fieldName : getFieldNames()) {
			if(isPK(fieldName)) {				
				names.add(fieldName);
			}else {
				
			}
		}
		return names;
	}
	

	private boolean  isPK(String fieldName) {
		DBTable dbTable = (DBTable) Config.allTable.get(this.tableName);
		boolean flag = false;
		for(ColumnInfoBean aBean : dbTable.pks) {
			if(fieldName.equalsIgnoreCase(aBean.getName())) {
				return true;
			}
		}
		return flag;
	}
	
	public List<ColumnInfoBean> getPKColumnInfos() {
		DBTable dbTable = (DBTable) Config.allTable.get(this.tableName);
		List<ColumnInfoBean> list = new ArrayList<ColumnInfoBean>();
		for(String fieldName : getPKNameList()) {
			ColumnInfoBean aColumnInfoBean = dbTable.fieldMap.get(fieldName);
			list.add(aColumnInfoBean);
		}		
		return list;
	}
	
	public List<String> getFieldNames() {
		List<String> list = new ArrayList<String>();
		Object [] objs = fieldMap.keySet().toArray();
		for(Object obj : objs) {
			list.add(obj.toString());
		}
		return list;
	}
	
	public String getUpdateSQL() {
		StringBuffer sbf = new StringBuffer();
		StringBuffer sbf2= new StringBuffer();
		sbf.append("update "+this.tableName+" set ");
		int i = 0;
		for(String fieldName : getNoPKNameList()) {
			i++;
			if(i==1) {
				sbf.append(fieldName+"=?");
			}else {
				sbf.append(", "+fieldName+"=?");
				
			}
		}
		sbf.append(" where ");
		i = 0;
		for(String fieldName : getPKNameList()) {
			i++;
			if(i==1) {
				sbf.append(fieldName+"=?");
			}else {
				sbf.append(" and  "+fieldName+"=?");
				
			}
		}
		return sbf.toString();
	}
	

	public boolean isFlag() {
		return flag;
	}

	/**
	 * 已经更新了主键
	 */
	public void setFlag() {
		this.flag = true;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	
	
	/**
	 * 转成HTML
	 */
	public String getHTML() {
		StringBuffer sbf = new StringBuffer();
		Iterator<String> iter  = fieldMap.keySet().iterator(); 		
		sbf.append("<table  name=\""+tableName+"\" operation = \""+op+"\">");
		sbf.append("<tr>");
		while(iter.hasNext()) {
			sbf.append("<td>");
			String fieldName = iter.next();
			sbf.append(fieldName);
			sbf.append("</td>");
			
		}
		sbf.append("</tr>");
		iter  = fieldMap.keySet().iterator(); 
		sbf.append("<tr>");
		while(iter.hasNext()) {
			sbf.append("<td>");
			String fieldName = iter.next();
			sbf.append(fieldMap.get(fieldName));
			sbf.append("</td>");			
		}
		sbf.append("</tr>");
		sbf.append("</table>");
		return sbf.toString();
		
	}
	
}
